<template>
	<view class="wrap">
		<u-form :model="dataForm" ref="uForm">
			<u-form-item label-position="left" label="上传图片:" prop="imgUrl" label-width="20%">
				<u-upload width="160" height="160" :action="uploadUrl" :header="headers" :fileList="fileList"
					@on-success="success" @on-remove="uploadDelete" :deletable="true" max-count="3">
				</u-upload>
			</u-form-item>
			<u-form-item label-position="left" label="发布类型:" prop="type" label-width="20%">
				<u-radio-group v-model="dataForm.postType" slot="right">
					<u-radio v-for="(item, index) in postTypeList" :key="index" :name="item.value">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label-position="left" label="宠物种类:" prop="type" label-width="20%">
				<u-radio-group v-model="dataForm.petBreed" slot="right">
					<u-radio v-for="(item, index) in petBreedList" :key="index" :name="item.value">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label-position="left" label="宠物性别:" prop="type" label-width="20%">
				<u-radio-group v-model="dataForm.petGender" slot="right">
					<u-radio v-for="(item, index) in petGenderList" :key="index" :name="item.value">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label-position="left" label="宠物名字:" prop="imgUrl" label-width="20%">
				<u-input v-model="dataForm.petName" />
			</u-form-item>
			<u-form-item label-position="left" label="丢失地址:" prop="imgUrl" label-width="20%">
				<u-input v-model="dataForm.lostAddress" />
			</u-form-item>
			<u-form-item label-position="left" label="所在城市:" prop="type" label-width="20%">
				<input v-model="dataForm.city" type="text" placeholder-class="line" placeholder="省市区县、乡镇等"
					@click="showRegionPicker()" />
			</u-form-item>
			<u-form-item label-position="left" label="丢失时间:" prop="type" label-width="20%">
				<input v-model="dataForm.lostTime" type="text" placeholder-class="line" placeholder="选择丢失时间"
					@click="showTimePicker()" />
			</u-form-item>
			<u-form-item label-position="left" label="宠物描述:" prop="intro" label-width="20%">
				<u-input type="textarea" :border="true" placeholder="请输入丢失宠物相关描述...:" v-model="dataForm.description" />
			</u-form-item>
			<u-form-item label-position="left" label="联系人姓名:" prop="imgUrl" label-width="30%">
				<u-input v-model="dataForm.contactName" />
			</u-form-item>
			<u-form-item label-position="left" label="手机号:" prop="imgUrl" label-width="30%">
				<u-input v-model="dataForm.contactPhone" @click="clickShowKeyboard()" />
			</u-form-item>
			<u-form-item label-position="left" label="微信号:" prop="imgUrl" label-width="30%">
				<u-input v-model="dataForm.contactWechat" />
			</u-form-item>
			<u-form-item label-position="left" label="是否显示电话号码:" prop="type" label-width="40%">
				<u-radio-group v-model="dataForm.showPhone" slot="right">
					<u-radio v-for="(item, index) in showPhoneList" :key="index" :name="item.value">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">发布</u-button>
		<u-picker mode="region" ref="uPicker" v-model="showRegion" @confirm="confirmRegionPicker" />
		<u-picker mode="time" ref="uPicker" v-model="showTime" @confirm="confirmTimePicker" />
		<u-keyboard ref="uKeyboard" mode="number" v-model="showKeyboard" @change="changeShowKeyboard"
			@backspace="backspace"></u-keyboard>
	</view>
</template>

<script>
	import {
		updatePetlost,
		getPetLostDetailById
	} from '@/api/me/changeMySeekingPetsDetail.js'
	export default {
		data() {
			return {
				uploadUrl: 'http://127.0.0.1:8181/pet-system/piclocal/upload/pic',
				headers: {
					token: uni.getStorageSync('token')
				},
				fileList: [],
				id: '',
				dataForm: {
					id: '',
					postType: 0,
					petBreed: 0,
					petGender: 0,
					petName: '',
					lostAddress: '',
					lostTime: '',
					city: '',
					description: '',
					contactName: '',
					contactPhone: '',
					contactWechat: '',
					showPhone: 0,
					imageUrl: '',
					imageUrlList: []
				},
				postTypeList: [{
						name: '寻找爱宠',
						value: 0,
						disabled: false
					},
					{
						name: '寻找主人',
						value: 1,
						disabled: false
					}
				],
				petBreedList: [{
						name: '狗狗',
						value: 0,
						disabled: false
					},
					{
						name: '猫咪',
						value: 1,
						disabled: false
					},
					{
						name: '其他',
						value: 2,
						disabled: false
					}
				],
				petGenderList: [{
						name: '男孩',
						value: 0,
						disabled: false
					},
					{
						name: '女孩',
						value: 1,
						disabled: false
					}
				],
				showPhoneList: [{
						name: '否',
						value: 0,
						disabled: false
					},
					{
						name: '是',
						value: 1,
						disabled: false
					}
				],
				showRegion: false,
				showTime: false,
				showKeyboard: false
			}
		},
		onLoad(e) {
			this.getData(e.id)
			this.id = e.id
		},
		methods: {
			clickShowKeyboard() {
				this.showKeyboard = true;
			},
			changeShowKeyboard(val) {
				this.dataForm.contactPhone += val;
			},
			backspace() {
				// 删除value的最后一个字符
				if (this.dataForm.contactPhone.length) {
					this.dataForm.contactPhone = this.dataForm.contactPhone.substr(0, this.dataForm.contactPhone.length - 1);
				}
			},
			showRegionPicker() {
				this.showRegion = true;
			},
			confirmRegionPicker(e) {
				this.dataForm.city = e.province.label + '-' + e.city.label + '-' + e.area.label
			},
			showTimePicker() {
				this.showTime = true;
			},
			confirmTimePicker(e) {
				this.dataForm.lostTime = e.year + '-' + e.month + '-' + e.day + ' 00:00:00'
			},
			success(data, index, lists, name) {
				this.dataForm.imageUrlList.push(data.data.imgAddress)
				this.dataForm.imageUrl = this.dataForm.imageUrlList.join(",")
				console.log(this.dataForm.imageUrl)
			},
			// 图标删除回调函数
			uploadDelete(index, lists, name) {
				console.log(index)
				this.dataForm.imageUrlList.splice(index, 1)
				this.dataForm.imageUrl = this.dataForm.imageUrlList.join(",")
				console.log(this.dataForm.imageUrl)
			},
			getData(id){
				getPetLostDetailById(id).then(res => {
					if (res.code == 0) {
						this.dataForm = res.data
						this.isCollect = res.data.isPetLostCollect === 0 ? false : true
						// 格式化成 uView 组件需要的格式
						this.fileList = res.data.imageUrlList.map((item, index) => ({
						    url: item, // 图片 URL
							name: `img-${index}`, // 需要唯一 name
						}));
					}
				})
			},
			submit() {
				console.log(this.dataForm)
				updatePetlost(this.dataForm).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '已成功修改！',
							icon: 'success'
						});
						this.dataForm = {}
						this.fileList = []
						// 延迟返回上一页，确保提示可以看到
						            setTimeout(() => {
						                uni.navigateBack();
						            }, 1500);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						});
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>